<template>
  <div class="user-center">
    <!-- 移动端顶部导航栏 -->
    <div class="mobile-header" v-if="isMobile">
      <div class="mobile-nav">
        <div class="mobile-user-info">
          <el-avatar :size="40" :src="user?.avatarUrl || '/images/avatars/default-80.png'" />
          <div class="mobile-user-details">
            <h3 class="mobile-username">{{ user?.username || '用户名' }}</h3>
            <p class="mobile-user-email">{{ user?.email || 'user@example.com' }}</p>
          </div>
        </div>
        <el-button 
          type="text" 
          class="mobile-menu-btn"
          @click="showMobileMenu = !showMobileMenu"
        >
          <el-icon><Menu /></el-icon>
        </el-button>
      </div>
      
      <!-- 移动端菜单抽屉 -->
      <el-drawer
        v-model="showMobileMenu"
        direction="rtl"
        size="280px"
        :with-header="false"
        class="mobile-menu-drawer"
      >
        <div class="mobile-menu-content">
          <div class="mobile-menu-header">
            <h3>用户中心</h3>
            <el-button type="text" @click="showMobileMenu = false">
              <el-icon><Close /></el-icon>
            </el-button>
          </div>
          <el-menu 
            :default-active="activeMenu" 
            class="mobile-sidebar-menu" 
            @select="handleMenuSelect"
          >
            <el-menu-item index="profile">
              <el-icon><User /></el-icon>
              <span>个人信息</span>
            </el-menu-item>
            <el-menu-item index="address">
              <el-icon><Location /></el-icon>
              <span>收货地址</span>
            </el-menu-item>
            <el-menu-item index="orders">
              <el-icon><Document /></el-icon>
              <span>我的订单</span>
            </el-menu-item>
            <el-menu-item index="wishlist">
              <el-icon><Star /></el-icon>
              <span>我的收藏</span>
            </el-menu-item>
            <el-menu-item index="security">
              <el-icon><Lock /></el-icon>
              <span>账户安全</span>
            </el-menu-item>
          </el-menu>
        </div>
      </el-drawer>
    </div>

    <el-container>
      <!-- 桌面端侧边栏 - 移动端隐藏 -->
      <el-aside width="250px" class="sidebar" v-if="!isMobile">
        <div class="user-info">
          <el-avatar :size="80" :src="user?.avatarUrl || '/images/avatars/default-80.png'" />
          <h3 class="username">{{ user?.username || '用户名' }}</h3>
          <p class="user-email">{{ user?.email || 'user@example.com' }}</p>
        </div>

        <el-menu :default-active="activeMenu" class="sidebar-menu" @select="handleMenuSelect">
          <el-menu-item index="profile">
            <el-icon><User /></el-icon>
            <span>个人信息</span>
          </el-menu-item>
          <el-menu-item index="address">
            <el-icon><Location /></el-icon>
            <span>收货地址</span>
          </el-menu-item>
          <el-menu-item index="orders">
            <el-icon><Document /></el-icon>
            <span>我的订单</span>
          </el-menu-item>
          <el-menu-item index="wishlist">
            <el-icon><Star /></el-icon>
            <span>我的收藏</span>
          </el-menu-item>
          <el-menu-item index="security">
            <el-icon><Lock /></el-icon>
            <span>账户安全</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main class="main-content" :class="{ 'mobile-main': isMobile }">
        <router-view v-slot="{ Component, route }">
          <transition name="user-slide" mode="out-in">
            <component :is="Component" :key="route.fullPath" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { User, Location, Document, Star, Lock, Menu, Close } from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

const STORAGE_KEY = 'userCenter.activeMenu'

// 响应式数据
const activeMenu = ref('profile')
const showMobileMenu = ref(false)
const isMobile = ref(false)

// 计算属性
const user = computed(() => authStore.user)

// 方法
const handleMenuSelect = (index: string) => {
  localStorage.setItem(STORAGE_KEY, index)
  router.push(`/user/${index}`)
  // 移动端选择菜单后自动关闭抽屉
  if (isMobile.value) {
    showMobileMenu.value = false
  }
}

const checkMobile = () => {
  isMobile.value = window.innerWidth <= 768
}

const initActiveMenu = () => {
  const path = route.path
  if (path.includes('/user/profile')) {
    activeMenu.value = 'profile'
  } else if (path.includes('/user/address')) {
    activeMenu.value = 'address'
  } else if (path.includes('/user/orders')) {
    activeMenu.value = 'orders'
  } else if (path.includes('/user/wishlist')) {
    activeMenu.value = 'wishlist'
  } else if (path.includes('/user/coupon')) {
    activeMenu.value = 'coupon'
  } else if (path.includes('/user/security')) {
    activeMenu.value = 'security'
  } else {
    // 退化到本地记忆
    const saved = localStorage.getItem(STORAGE_KEY)
    activeMenu.value = saved || 'profile'
  }
}

// 监听路由变化，同步选中项与本地记忆
watch(() => route.path, () => {
  initActiveMenu()
  const current = activeMenu.value
  if (current) localStorage.setItem(STORAGE_KEY, current)
}, { immediate: true })

// 组件挂载时初始化
onMounted(() => {
  initActiveMenu()
  checkMobile()
  window.addEventListener('resize', checkMobile)
})

// 组件卸载时清理事件监听器
onUnmounted(() => {
  window.removeEventListener('resize', checkMobile)
})
</script>

<style scoped>
.user-center {
  min-height: calc(100vh - 64px);
  background-color: #f8fafc;
}

/* 移动端顶部导航栏 */
.mobile-header {
  display: none;
  background: white;
  border-bottom: 1px solid #e9ecef;
  padding: 16px 20px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.mobile-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.mobile-user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.mobile-user-details {
  display: flex;
  flex-direction: column;
}

.mobile-username {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 2px 0;
}

.mobile-user-email {
  font-size: 12px;
  color: #909399;
  margin: 0;
}

.mobile-menu-btn {
  padding: 8px;
  font-size: 20px;
}

/* 移动端菜单抽屉 */
.mobile-menu-drawer {
  :deep(.el-drawer__body) {
    padding: 0;
  }
}

.mobile-menu-content {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.mobile-menu-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #e9ecef;
}

.mobile-menu-header h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
}

.mobile-sidebar-menu {
  border: none;
  flex: 1;
}

/* 桌面端侧边栏 */
.sidebar {
  background: white;
  border-right: 1px solid #e9ecef;
  padding: 20px 0;
}

.user-info {
  text-align: center;
  padding: 20px;
  border-bottom: 1px solid #e9ecef;
  margin-bottom: 20px;
}

.username {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 12px 0 4px 0;
}

.user-email {
  font-size: 14px;
  color: #909399;
  margin: 0;
}

.sidebar-menu {
  border-right: none;
}

.main-content {
  padding: 20px;
  position: relative;
}

.main-content.mobile-main {
  padding: 16px;
}

/* 路由切换动画 - 上下滑入滑出 */
:deep(.user-slide-enter-active),
:deep(.user-slide-leave-active) {
  transition: transform 0.25s ease, opacity 0.25s ease;
}

/* 新页面进场 */
:deep(.user-slide-enter-from) {
  opacity: 0;
  transform: translateY(16px);
}
:deep(.user-slide-enter-to) {
  opacity: 1;
  transform: translateY(0);
}

/* 旧页面离场 */
:deep(.user-slide-leave-from) {
  opacity: 1;
  transform: translateY(0);
}
:deep(.user-slide-leave-to) {
  opacity: 0;
  transform: translateY(-16px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .mobile-header {
    display: block;
  }
  
  .sidebar {
    display: none;
  }
  
  .main-content {
    width: 100% !important;
    margin-left: 0 !important;
  }
  
  .user-center {
    flex-direction: column;
  }
}

@media (min-width: 769px) {
  .mobile-header {
    display: none;
  }
  
  .sidebar {
    display: block;
  }
}
</style>
